package com.APISecond.NewDate;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class Instant_ {
    public static void main(String[] args) {
        Instant now = Instant.now();
        System.out.println(now);
        //获取秒数
        long Second=now.getEpochSecond();
        System.out.println(Second);
        long None=now.getNano();
        System.out.println(None);
        Instant instant=now.plusSeconds(200);//加秒数
        //判断时间前后
        System.out.println(instant.isAfter(now));
        System.out.println("--------------");
        //格式化时间
        //创建格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");//方法一
        //对时间格式化
        LocalDateTime now1 = LocalDateTime.now();
        System.out.println(now1);
        String rs = now1.format(formatter);
        System.out.println(rs);//第一种方法反向格式
        String rs2=now1.format(formatter);
        System.out.println(rs2);//反向格式化
        //解析时间
        String dateStr="2023年11月28日 12时12分11秒";
        LocalDateTime ldt=LocalDateTime.parse(dateStr,formatter);//方法二
        System.out.println(ldt);
        System.out.println("-------------");
        //Period可以计算两个LocalDate相差的年月日。不能精确到时间。
        LocalDate start = LocalDate.of(2023, 11, 28);
        LocalDate end = LocalDate.of(2023, 11, 30);
        Period period=Period.between(start,end);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
        //Duration计算天，小时，分，秒，纳秒的差。
        LocalDateTime time = LocalDateTime.of(2023, 11, 28, 17, 23, 25);
        LocalDateTime time1 = LocalDateTime.of(2023, 11, 28, 19, 45, 12);
        Duration duration = Duration.between(time, time1);
        System.out.println(duration.toDays());
        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());
    }
}
